package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class KunMing extends City {

	private Handler request1handler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int index=body.indexOf("未处理违法次数：</td>");
				if (index>0){
					String total=body.substring(index+13).split("</td>")[0].trim().split(">")[1].trim();
					int t=Integer.parseInt(total);
					callback.onComplete(t, String.format("ret:很抱歉，您有%d个违章，但我们无法给您提供更详细的信息", t));
				}
				else{
					index=body.indexOf("class=\"box04\">");
					if (index>0){
						String msg=body.substring(index+14).split("<br>")[0].trim();
						callback.onComplete(-10002, "ret:"+msg);
					}
					else{
						callback.onComplete(-10001, "");
					}
				}
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,request1handler,"UTF-8").post("http://www.km122.cn//q2/soapclient.php", 
					String.format("stype=vehvio&carfirst=%s&carnum=%s&ctype=02&fadongji=%s&button=",
							URLEncoder.encode("云","GBK"),chepai.substring(1),values[0]));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("云A");
	}

	@Override
	public String[] paramDesc() {
		String[] s={"发动机号后四位"};
		return s;
	}

}
